#include <iostream>
#include <cstring>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/epoll.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>

#define MAX_EVENTS 10
#define MAXLINE 1024

int main()
{
    // 创建socket对象
    int sockfd = socket(AF_INET, SOCK_STREAM, 0);
    if (sockfd < 0)
    {
        perror("socket creation failed");
        return 1;
    }

    // 创建网络通信对象
    struct sockaddr_in addr;
    addr.sin_family = AF_INET;
    addr.sin_port = htons(8888);
    addr.sin_addr.s_addr = INADDR_ANY;

    // 绑定socket对象与通信链接
    if (bind(sockfd, (struct sockaddr *)&addr, sizeof(addr)) < 0)
    {
        perror("bind failed");
        return 1;
    }

    // 监听连接
    listen(sockfd, 5);

    // 创建epoll对象
    int epollfd = epoll_create1(0);
    if (epollfd == -1)
    {
        perror("epoll_create1 failed");
        return 1;
    }

    // 添加监听的socket到epoll对象中
    struct epoll_event event;
    event.events = EPOLLIN;
    event.data.fd = sockfd;
    if (epoll_ctl(epollfd, EPOLL_CTL_ADD, sockfd, &event) == -1)
    {
        perror("epoll_ctl EPOLL_CTL_ADD failed");
        return 1;
    }

    struct epoll_event events[MAX_EVENTS];

    while (1)
    {
        // 等待事件发生
        int num_events = epoll_wait(epollfd, events, MAX_EVENTS, -1);
        if (num_events == -1)
        {
            perror("epoll_wait failed");
            return 1;
        }

        // 处理发生的事件
        for (int i = 0; i < num_events; ++i)
        {
            if (events[i].data.fd == sockfd)
            {
                // 如果是新的连接请求
                struct sockaddr_in cli;
                socklen_t len = sizeof(cli);
                int newsockfd = accept(sockfd, (struct sockaddr *)&cli, &len);
                if (newsockfd < 0)
                {
                    perror("ERROR on accept");
                    continue;
                }

                // 将新的连接加入到epoll对象中
                event.events = EPOLLIN;
                event.data.fd = newsockfd;
                if (epoll_ctl(epollfd, EPOLL_CTL_ADD, newsockfd, &event) == -1)
                {
                    perror("epoll_ctl EPOLL_CTL_ADD newsockfd failed");
                    close(newsockfd);
                    continue;
                }
            }
            else
            {
                // 如果是已连接socket上的数据可读事件
                int clientfd = events[i].data.fd;

                // 接收客户端发送过来的文件路径
                char filepath[MAXLINE];
                ssize_t n = recv(clientfd, filepath, MAXLINE - 1, 0);
                if (n <= 0)
                {
                    perror("接收失败");
                    close(clientfd);
                    continue;
                }
                filepath[n] = '\0';
                printf("接收到的文件路径：%s\n", filepath);

                // 尝试打开文件
                FILE *file = fopen(filepath, "r");

                if (file != NULL)
                { // 文件存在，发送文件内容给客户端
                    printf("文件打开成功\n");
                    char buffer[MAXLINE];
                    size_t bytesRead;
                    while ((bytesRead = fread(buffer, 1, MAXLINE, file)) > 0)
                    {
                        send(clientfd, buffer, bytesRead, 0);
                    }
                    fclose(file);     // 关闭文件
                    close(clientfd); // 关闭连接
                }
                else
                {
                    printf("文件打开失败\n");
                    // 文件不存在，发送提示消息给客户端
                    char reply[] = "file not exist";
                    send(clientfd, reply, strlen(reply), 0);
                    close(clientfd); // 关闭连接
                }
            }
        }
    }

    close(sockfd);
    return 0;
}
